package com.gua.encrypt.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.gua.common.exception.BusinessExecption;
import com.gua.common.request.BaseController;
import com.gua.common.response.AjaxResult;
import com.gua.encrypt.request.UrlReqUrlParamVO;
import com.gua.encrypt.response.UrlReqUrlRespVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 
 * @author huangjj
 *
 */
@Api(value = "url编码/解码", tags = { "url编码/解码" })
@RestController
public class UrlEncodeController extends BaseController {

	@ApiOperation(value = "url编码")
	@PostMapping("/url/doUrlEncode")
	public AjaxResult<UrlReqUrlRespVO> doUrlEncode(@RequestBody @Valid UrlReqUrlParamVO urlReqUrlVO) {
		UrlReqUrlRespVO resp = new UrlReqUrlRespVO();
		try {
			resp.setUrlTargetData(URLEncoder.encode(urlReqUrlVO.getUrlSourceData(), 
					StandardCharsets.UTF_8.name()));
			return success(resp);
		} catch (UnsupportedEncodingException e) {
			throw new BusinessExecption("url编码异常");
		}
	}

	@ApiOperation(value = "url解码")
	@PostMapping("/url/doUrlDecode")
	public AjaxResult<UrlReqUrlRespVO> doUrlDecode(@RequestBody @Valid UrlReqUrlParamVO urlReqUrlVO) {
		UrlReqUrlRespVO resp = new UrlReqUrlRespVO();
		try {
			resp.setUrlTargetData(URLDecoder.decode(urlReqUrlVO.getUrlSourceData(), 
					StandardCharsets.UTF_8.name()));
			return success(resp);
		} catch (UnsupportedEncodingException e) {
			throw new BusinessExecption("url解码异常");
		}
	}

}
